package com.cwj.film.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cwj.common.domain.film;
import com.cwj.common.utils.utils;
import com.cwj.film.service.inter.search;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Service
public class searchImpl implements search {
    @Resource
    private com.cwj.film.dao.filmDao filmDao;
    @Resource
    private ServletContext servletContext;
    @Override
    public List<film> GetFilmListByTitle(String title,int size) throws InvocationTargetException, IllegalAccessException {
        QueryWrapper<film> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("type", "title","fid","release_date","location","film_source")
                        .like("title","%"+title+"%").last("limit 0 , "+size);
        List<film> films = filmDao.selectList(queryWrapper);
        utils.GetFilmPictList(films,540,488,servletContext.getRealPath(""));

        return films;
    }
    @Override
    public Integer GetFilmSizeByTitle(String title){
        QueryWrapper<film> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title","%"+title+"%");
        return filmDao.selectCount(queryWrapper);

    }

    @Override
    public List<film> GetFilmListByPageTitle(String title, int size, int page) throws InvocationTargetException, IllegalAccessException {
        int start=(page-1)*size;

        QueryWrapper<film> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("title","%"+title+"%").last(" limit "+start+" , "+size).select("type", "title","fid","release_date","location","film_source");
        List<film> films = filmDao.selectList(queryWrapper);
        utils.GetFilmPictList(films,540,488,servletContext.getRealPath(""));

        return films;
    }
}
